package com.sinaapp.zygl.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.sinaapp.zygl.bean.Customer;
import com.sinaapp.zygl.dao.CustomerDao;
import com.sinaapp.zygl.dao.impl.CustomerDaoImpl;

public class CustomerAction extends ActionSupport {

	private static final long serialVersionUID = 328580540775229602L;

	@Override
	public String execute() throws Exception {
		CustomerDao dao = new CustomerDaoImpl();
		List<Customer> cpagelist = new ArrayList<Customer>();
		Customer customer = new Customer();
		customer.setCid(cid);
		customer.setCname(cname);
		customer.setCgender(cgender);
		customer.setCaddress(caddress);
		customer.setCbirth(cbirth);
		customer.setCtel(ctel);
		customer.setCcompany(ccompany);
		customer.setCposition(cposition);
		customer.setCbirthStart(cbirthStart);
		customer.setCbirthEnd(cbirthEnd);
		cpagelist = dao.getCustomers(page, rows, sort, order, customer);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", dao.queryCustomerTotal(customer));
		m.put("rows", cpagelist);
		writeJson(m);
		return null;
	}

	public String add() throws Exception {
		CustomerDao dao = new CustomerDaoImpl();
		Customer customer = new Customer();
		customer.setCname(cname);
		customer.setCgender(cgender);
		customer.setCaddress(caddress);
		customer.setCbirth(cbirth);
		customer.setCtel(ctel);
		customer.setCcompany(ccompany);
		customer.setCposition(cposition);
		Map<String, Object> m = new HashMap<String, Object>();
		if (dao.add(customer)) {
			m.put("success", true);
			m.put("customer", customer);
		} else {
			m.put("success", false);
			m.put("customer", customer);
		}

		writeJson(m);
		return null;
	}

	public String update() throws Exception {
		CustomerDao dao = new CustomerDaoImpl();
		Customer customer = new Customer();
		customer.setCid(cid);
		customer.setCname(cname);
		customer.setCgender(cgender);
		customer.setCaddress(caddress);
		customer.setCbirth(cbirth);
		customer.setCtel(ctel);
		customer.setCcompany(ccompany);
		customer.setCposition(cposition);
		Map<String, Object> m = new HashMap<String, Object>();
		if (dao.update(customer)) {
			m.put("success", true);
			m.put("customer", customer);
		} else {
			m.put("success", false);
			m.put("customer", customer);
		}
		writeJson(m);
		return null;
	}

	public String delete() throws Exception {
		CustomerDao dao = new CustomerDaoImpl();
		Map<String, Object> m = new HashMap<String, Object>();
		if (dao.delete(cid)) {
			m.put("cid", cid);
			m.put("success", true);
		} else {
			m.put("cid", cid);
			m.put("success", false);
		}
		writeJson(m);
		return null;
	}

	public void writeJson(Object o) {
		// String json = getJsonString(o);
		try {
			// ServletActionContext.getResponse().getWriter().write(json);
			String json = JSON.toJSONStringWithDateFormat(o, "yyyy-MM-dd HH:mm:ss");
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
			ServletActionContext.getResponse().getWriter().close();
		} catch (Exception e) {
		}
	}

	private int page;
	private int rows;
	private String sort;
	private String order;

	private String cid;
	private String cname;
	private String cgender;
	private String caddress;
	private Date cbirth;
	private String ctel;
	private String ccompany;
	private String cposition;

	private Date cbirthStart;
	private Date cbirthEnd;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String getCgender() {
		return cgender;
	}

	public void setCgender(String cgender) {
		this.cgender = cgender;
	}

	public String getCaddress() {
		return caddress;
	}

	public void setCaddress(String caddress) {
		this.caddress = caddress;
	}

	public Date getCbirth() {
		return cbirth;
	}

	public void setCbirth(Date cbirth) {
		this.cbirth = cbirth;
	}

	public String getCtel() {
		return ctel;
	}

	public void setCtel(String ctel) {
		this.ctel = ctel;
	}

	public String getCcompany() {
		return ccompany;
	}

	public void setCcompany(String ccompany) {
		this.ccompany = ccompany;
	}

	public String getCposition() {
		return cposition;
	}

	public void setCposition(String cposition) {
		this.cposition = cposition;
	}

	public Date getCbirthStart() {
		return cbirthStart;
	}

	public void setCbirthStart(Date cbirthStart) {
		this.cbirthStart = cbirthStart;
	}

	public Date getCbirthEnd() {
		return cbirthEnd;
	}

	public void setCbirthEnd(Date cbirthEnd) {
		this.cbirthEnd = cbirthEnd;
	}
}
